<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4285F4;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #3367D6;
        }
        .error-message {
            color: red;
            font-size: 14px;
            margin-top: 5px;
        }
        .success-message {
            color: green;
            font-size: 14px;
            margin-top: 5px;
        }
        .register-link {
            text-align: center;
            margin-top: 15px;
        }
        .register-link a {
            color: #4285F4;
            text-decoration: none;
        }
        .register-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>用户登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <button type="button" onclick="login()">登录</button>
        </div>
        <div id="message" class="error-message"></div>
        <div class="register-link">
            还没有账号？<a href="/register.html">立即注册</a>
        </div>
    </form>
</div>

<script>
    function login() {
        const form = document.getElementById('loginForm');
        const messageDiv = document.getElementById('message');

        // 清除之前的信息
        messageDiv.textContent = '';
        messageDiv.className = 'error-message';

        // 获取表单数据
        const username = form.username.value.trim();
        const password = form.password.value.trim();

        // 前端验证
        if (!username || !password) {
            messageDiv.textContent = '用户名和密码不能为空';
            return;
        }

        // 发送登录请求
        fetch('/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                userName: username,
                password: password
            })
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应异常');
                }
                return response.json();
            })
            .then(data => {
                if (data.code === 1) {
                    // 登录成功
                    messageDiv.textContent = '登录成功！正在跳转...';
                    messageDiv.className = 'success-message';

                    // 存储用户信息（可选）
                    if (data.data) {
                        localStorage.setItem('user', JSON.stringify(data.data));
                    }

                    // 跳转到首页
                    setTimeout(() => {
                        window.location.href = '/home.html';
                    }, 1000);
                } else {
                    // 登录失败
                    messageDiv.textContent = data.msg || '用户名或密码错误';
                }
            })
            .catch(error => {
                console.error('登录错误:', error);
                messageDiv.textContent = '登录失败: ' + error.message;
            });
    }

    // 添加回车键登录功能
    document.getElementById('password').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            login();
        }
    });
</script>
</body>
</html>